package com.scalar.chiptrack.operations.finishedGoods.actionCommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.finishedGoods.forms.FinishedGoodsSearchForm;
import com.scalar.chiptrack.operations.finishedGoods.dao.FinishedGoodsDAO;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;


/**
 * Processes the request for searching the Blank FGI
 *
 * @author Varma
 * @version 1.0
 */
public class FinishedGoodsGetFgiPartsCommand extends ActionCommand
{
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        FinishedGoodsSearchForm finishedGoodsSearchForm = (FinishedGoodsSearchForm) form;
        ActionForward actionForward       = mapping.findForward( "finishedGoodsSearch" );;
        HttpSession mSession              = request.getSession();
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        ArrayList fgiProductsList         = new ArrayList();
        try
        {
            fgiProductsList = FinishedGoodsDAO.getFgiProducts( finishedGoodsSearchForm.getBaseProductId() );
        }
        catch( ScalarException e)
        {
            errors.put( "Couldn't fetch Base Products List from Database !", null);
        }
        if( fgiProductsList == null && fgiProductsList.isEmpty())
        {
            errors.put( "Couldn't find any fgi products for this base product ", null);
        }


        return actionForward;
    }
}